#!/bin/python
# -*- coding: gb2312; mode:python ; tab-width:4 -*- ex:set tabstop=4 shiftwidth=4 expandtab: -*-

##########################################################
# PROJECT : WSN TestSuite Client
# FILE    : ClientApp.py
# CREATION: 2007-09-17
# LICENSE : GPL
# $Id$
##########################################################
"""
973 WSN Test Suite

Object: Client Application
Usage : code entrance
Author: Wei Huangfu
Date  : 2007-09-17
"""

__author__ = "huangfuwei@is.iscas.ac.cn"
__copyright__ = "Copyright (C) 2006-2007 ISCAS"

__revision__ = "$Revision$"
__version__ = "2.0"

##
import serialport_source
import service_thread

import logging
import time

from ClientMessageSource import ClientMessageSource
import MessageDB

import timeout_thread
import snmp_thread


class aaaa():
	def __init__(self,r):
		baudrate=115200		 
		self.socketthread = service_thread.ServiceThread(self.processDataMessages)
        self.socketthread.start()
        self.timeoutthread = timeout_thread.timeoutThread(self.socketthread.clientMessageSource)
        self.timeoutthread.start()
        self.saveFile = MessageDB.MessageDB(self.socketthread.clientMessageSource)
        #changyibiao
        device = serialport_source.Source("/dev/ttyTX0", 9600,notify=None,messagesource=self.socketthread.clientMessageSource)
        self.socketthread.appendDevice(device)
        self.serialExistFlag = True
        self.saveFlag = True
        
    def process(self,e):
    	pass
		
        
    def processDataMessages(self,e):
        
        if self.socketthread:
            try:
                #print "in processDataMessages"
                rawMessages = self.socketthread.clientMessageSource.getMessages()
                
            except:
                
                logging.error(_("Error in Get Messages from WSN"))
                return # if Exception, jump away!
            messages = [m[1] for m in rawMessages if m[0]=='DATA']
            if not messages:
                return
            if self.saveFlag: # store it?
                try:
                    self.saveFile.batchInsertMsg(messages)
                except:
                    print "Error!messages:",messages
                    
                    logging.error(_("Error in Insert Messages to file."))

		else:
			pass
if __name__=="__main__":
	aa=aaaa(1)
	